package com.study.talk.ui.app.friend

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.study.common.ToastUtil
import com.study.talk.ui.app.base.BaseViewModel
import com.study.talk.data.model.UserInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AddFriendViewModel @Inject constructor(application: Application) :
    BaseViewModel(application) {

    private val _users = MutableLiveData<List<UserInfo>>()
    val users: LiveData<List<UserInfo>> get() = _users

    fun queryUsers(keyword: String) {
        viewModelScope.launch {
            val result = userRepository.searchUsers(keyword)
            if (result.isEmpty()) {
                ToastUtil.showToast(app, "暂无符合条件的用户...")
            } else {
                result.let { _users.postValue(it) }
            }
        }
    }
}